package com.icode.gulimall.cart.controller;

import com.icode.common.utils.R;
import com.icode.gulimall.cart.interceptor.CartInterceptor;
import com.icode.gulimall.cart.service.CartService;
import com.icode.gulimall.cart.vo.CartItemVo;
import com.icode.gulimall.cart.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import java.util.concurrent.ExecutionException;


/**
 * @Author: my_code
 * @Email: my_code@foxmail.com
 * @Create: 2021-01-08 16:26
 */
@Controller
public class CartController {

    @Autowired
    CartInterceptor cartInterceptor;

    @Autowired
    CartService cartService;

    /**
     * 获取全部购物车
     * @param model
     * @return
     * @throws ExecutionException
     * @throws InterruptedException
     */
    @RequestMapping("/cart.html")
    public String cartList(Model model) throws ExecutionException, InterruptedException {
        CartVo cartVo = cartService.getCart();
        model.addAttribute("cart",cartVo);

        return "cartList";
    }

    /**
     * 添加购物项
     * @param skuId
     * @param num
     * @param attributes
     * @return
     * @throws ExecutionException
     * @throws InterruptedException
     */
    @GetMapping("/addCartItem")
    public String addCartItem(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num, RedirectAttributes attributes) throws ExecutionException, InterruptedException {

        cartService.addToCart(skuId, num);
        attributes.addAttribute("skuId", skuId);
        return "redirect:http://cart.gulimall.com/addToCartSuccessPage.html";
    }

    /**
     * 跳转到添加购物车成功页面
     * @param skuId
     * @param model
     * @return
     */
    @GetMapping(value = "/addToCartSuccessPage.html")
    public String addToCartSuccessPage(@RequestParam("skuId") Long skuId, Model model) {
        //重定向到成功页面。再次查询购物车数据即可
        CartItemVo cartItemVo = cartService.getCartItem(skuId);
        model.addAttribute("cartItem",cartItemVo);
        return "success";
    }

    /**
     * 商品数量改变
     * @param skuId
     * @param num
     * @return
     */
    @PostMapping("/countItem")
    @ResponseBody
    public void countItem(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num){
        cartService.changeCartItemNum(skuId, num);
    }

    @ResponseBody
    @PostMapping("/deleteItem")
    public void deleteItem(@RequestParam("skuId") List<Long> skuId){
        cartService.deleteItem(skuId);
    }

    /**
     * check = 1 商品选中、 check = 0未选中
     * @param skuId
     * @param check
     * @return
     */
    @ResponseBody
    @PostMapping("checkItem")
    public void checkItem(@RequestParam("skuId") Long skuId, @RequestParam("checked") Integer check){
        cartService.changeCartItemCheck(skuId,check);
    }

    @ResponseBody
    @GetMapping("getCheckedItems")
    public R getCheckedItems(){
        List<CartItemVo> checkedItems = cartService.getCheckedItems();
        return R.ok().setData(checkedItems);
    }
}
